package com.goods.shop.test;

import jdk.nashorn.internal.ir.Block;

import java.lang.reflect.Method;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
class Attribute{
    int num;

    public Attribute(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
class QueueProduce implements Runnable{
    private LinkedBlockingQueue queue;
    QueueProduce(LinkedBlockingQueue queue){
        this.queue=queue;
    }

    public void run(){
        int i=0;
        while(true){
            try{
                Thread.sleep(3000);
            }catch(Exception e){

            }

            i++;
            System.out.println("PUT:Thread:"+Thread.currentThread().getName()+"Data:"+i);
            this.queue.add("Thread:"+Thread.currentThread().getName()+"Data:"+i);
        }
    }
}
class QueueConsume implements Runnable{
    private LinkedBlockingQueue<String> queue;
    QueueConsume(LinkedBlockingQueue queue){
        this.queue=queue;
    }

    public void run(){
        int i=0;
        while(true){
            if(!queue.isEmpty()) {
                String top = queue.poll();
                System.out.println("取出:" + top);
            }else{
                System.out.println("为空");
            }
            try {
                Thread.sleep(200);
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
}
public class Person {
    public int getAge(){
        return 10;
    }
    public void setAge(int age){
        System.out.println("age==="+age);
    }
    public String say(int num){
        return "He can say :"+num;
    }


    public Integer age(String name){
        System.out.println(name);
        return 25;
    }
    public boolean judge(int age){
        return age>10;
    }

    public static void main(String[] args) {
//        Person person=new Person();
//        Supplier<Integer> ageSupplier=person::getAge;
//        System.out.println(ageSupplier.get());
//
//        Consumer<Integer> ageConsumer=person::setAge;
//        ageConsumer.accept(20);
//
//        Function<Integer,String> sayFunction=person::say;
//        String result=sayFunction.apply(10).toString();
//
//        System.out.println(result);
//
//        Function<String,Integer> ageFunction=person::age;
//        Integer resultAge=ageFunction.apply("wangyi");
//        System.out.println(resultAge);
//
//
//        Predicate<Integer> agePredicate=person::judge;
//        System.out.println(agePredicate.test(100));
//
//        Function<String,String> codeFunction=String::toUpperCase;
//        String codeResult=codeFunction.apply("abcdef");
//        System.out.println(codeResult);

//        List<Attribute> lists=new ArrayList<>();
//        for(int i=0;i<10;i++){
//            lists.add(new Attribute(i));
//        }
//        Consumer<Attribute> consumer=System.out::print;
//        consumer.accept(new Attribute(100));
//        consumer=vo->{
//            System.out.println("vo.num="+vo.getNum());
//            vo.setNum(vo.getNum()+vo.getNum());
//            System.out.println("vo.num="+vo.getNum());
//        };
//        lists.stream().filter(vo->vo.getNum()>5).forEach(consumer);
//        lists.forEach(vo->{
//            System.out.println(vo.getNum());
//        });
//        lists.forEach(System.out::print);

//        LinkedBlockingQueue<String> queue=new LinkedBlockingQueue<>();
//        QueueProduce produce1=new QueueProduce(queue);
//        QueueProduce produce2=new QueueProduce(queue);
//        QueueConsume consume=new QueueConsume(queue);
//        ExecutorService service= Executors.newCachedThreadPool();
//        service.execute(produce1);
//        service.execute(produce2);
//        service.execute(consume);
//        service.isShutdown();

        Instant start=Instant.now();
        Person person=new Person();
        int times=1000000;
        for(int i=0;i<times;i++){
            try {
                Method sayMethod=person.getClass().getMethod("say",int.class);
                sayMethod.setAccessible(true);
                sayMethod.invoke(person,100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Instant end=Instant.now();
        System.out.println("反射：花费时间:{}"+(end.toEpochMilli()-start.toEpochMilli()));

        for(int i=0;i<times;i++){
            Person p=new Person();
            p.say(100);
        }
        end=Instant.now();
        System.out.println("实例化:花费时间:{}"+(end.toEpochMilli()-start.toEpochMilli()));

    }
}
